<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class comment extends MY_Controller {
	function __construct(){
		parent::__construct();
		if(!$this->ion_auth->logged_in()){
			redirect(base_url("user/login"),"refresh");
		}
	}

	function reply(){
			$this->load->model("comment_model","commentModel");
			$_post = $this->input->post();
			if(empty($_post["pos_id"]) OR empty($_post['content'])){
				log_message('error', 'pos_id or content not found at '.__FILE__.':'.__LINE__);
				show_error('Error! ไม่สามารถเพิ่มความเห็นได้');
			}
			if(!empty($_post['content'])){
				$user_data = $this->ion_auth->user_array();
				$where['where']['id'] = $_post["pos_id"];
				if($_post["type"] == "forum"){
					$this->load->model("forum_model","forumModel");
					$dataPos = $this->forumModel->get($where);
				}else if($_post["type"] == "post"){
					$this->load->model("post_model","postModel");
					$dataPos = $this->postModel->get($where);
				}else{
					log_message('error', 'Can\'t find post_id at '.__FILE__.':'.__LINE__);
					show_error('Error! ไม่สามารถเพิ่มความเห็นได้');
				}
				$_post["title"] = "RE: ".$dataPos[0]["title"];
				$_post["status"] = 1;
				$_post["usr_id"] = $user_data["id"];
				$_post["usr_ip"] = $this->input->ip_address();
				$result = $this->commentModel->add($_post);
			}
			
			
			if($result){
				//Set date for update post
				$updatePost["id"] = $_post["pos_id"];
				$updatePost["update_date"] = date('Y-m-d H:i:s');
				if($_post["type"] == "forum"){
					$updateTime = $this->forumModel->add($updatePost);
				}else if($_post["type"] == "post"){
					$updateTime = $this->postModel->add($updatePost);
				}
				redirect(base_url($_post["type"]."/view/".urlencode($dataPos[0]["url"])));
			}else{
				redirect(base_url($_post["type"]."/view/".urlencode($dataPos[0]["url"])."?error=01"));
			}
	}

	function edit_comment($id = ""){
		$this->load->model("comment_model","commentModel");
		$where['where']['id'] = $id;
		$data = $this->commentModel->get($where);
		$userdata = $this->session->userdata("user_data");
		$this->_assign("userdata",$userdata);
		$this->_assign("data",$data);
		$this->_fetch("edit_comment");
	}


	function delete_comment($id =""){
		$this->load->model("comment_model","commentModel");
		$dataCom = $this->commentModel->get($id);	
		if($dataCom[0]['status'] == 1){
			$dataCom['id']= $id;
			$dataCom['status'] = 0;
			$this->commentModel->add($dataCom);
			$this->load->model("post_model","postModel");
			$where['where']['id'] = $dataCom[0]['pos_id'];
			$dataPos = $this->postModel->get($where);
		}elseif($dataCom[0]['status'] == 0){
			$dataCom['id']= $id;
			$dataCom['status'] = 1;
			$this->commentModel->add($dataCom);
			$this->load->model("post_model","postModel");
			$where['where']['id'] = $dataCom[0]['pos_id'];
			$dataPos = $this->postModel->get($where);
		}
		redirect(base_url("post/view/".urlencode($dataPos[0]["url"])));
	}

}